package hanjia;


import suanfa.ListNode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * Description:
 * Author:    Dane
 * CreateTime:2025/1/18-下午6:39
 * Since  :   1.0
 * Version:   1.0
 */
public class heima {


    public int binarySearch(int nums[], int target) {
        return Arrays.binarySearch(nums, target);

    }


    public static void remove(int index, int[] nums) {
        System.arraycopy(nums, index + 1, nums, index, nums.length - index - 1);
        nums[nums.length - 1] = 0;
    }


    public static void insert(int nums[]) {

        for (int i = 1; i < nums.length; i++) {
            int insert = nums[i];
            int sorted = i - 1;
            while (sorted >= 0 && nums[sorted] > insert) {
                nums[sorted + 1] = insert;
                sorted--;

            }
            nums[sorted + 1] = insert;
        }


    }


    public static void pop(int nums[]) {

        for (int i = 0; i < nums.length - 1; i++) {
            for (int j = i + 1; j < nums.length; j++) {
                if (nums[j] < nums[i]) {
                    int temp = nums[i];
                    nums[i] = nums[j];
                    nums[j] = temp;
                }

            }

        }

    }


    public static void hannuota(char a, char b, char c, int n) {
        if (n == 1) {
            System.out.println("第1个盘子" + a + "移动到" + c);
        } else {
            hannuota(a, c, b, n - 1);
            System.out.println("第" + n + "个盘子" + a + "移动到" + c);
            hannuota(b, a, c, n - 1);
        }


    }

    public static void yangHuiSanJiao(int nums[][]) {
        nums[0][0] = 1;
        for (int i = 1; i < nums.length; i++) {
            for (int j = 0; j < i + 1; j++) {
                if (j == 0) {
                    nums[i][j] = 1;
                    continue;
                }
                nums[i][j] = nums[i - 1][j] == 0 ? 1 : nums[i - 1][j - 1] + nums[i - 1][j];
            }
        }


    }

    //新链表
    public static ListNode reverseNode(ListNode node) {
        if (node == null) {
            return null;
        }

        ListNode temp = node;
        ListNode pre = null;
        while (temp != null) {
//            pre = new LinkedNode(temp.value, pre);
            ListNode newNode = new ListNode(temp.val, null);
            newNode.next=pre;
            pre=newNode;
            temp = temp.next;


        }
        return pre;
    }

    public LinkedNode deleteNode(LinkedNode node, int val) {
        if (node == null) {
            return null;
        }


        LinkedNode node1 = node;
        LinkedNode node2 = node.next;
        while (node2 != null) {

            if (node2.value == val) {
                node2 = node2.next;
                node1.next = node2;
            } else {
                node1 = node2;
                node2 = node2.next;

            }


        }
        if (node.value == val) {
            return node.next;
        }


        return node;

    }

    public LinkedNode deleteNode2(LinkedNode node, int val) {


        if (node == null) {
            return null;
        }
        if (node.value == val) {
            return deleteNode2(node.next, val);
        } else {
            node.next = deleteNode2(node.next, val);
            return node;
        }


    }

    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode listNode = new ListNode();
        listNode.next = head;
        findDaoShu(listNode, n);
        return listNode.next;
    }


    public static int findDaoShu(ListNode head, int n) {
        if (head == null) {
            return 0;
        }
        int sort = findDaoShu(head.next, n);

        if (sort == n) {
            head.next = head.next.next;
        }


        return sort + 1;

    }

    public ListNode removeNthFromEnd2(ListNode head, int n) {
        ListNode s = new ListNode();
        ListNode p1 = s;
        ListNode p2 = s;
        for (int i = 0; i < n + 1; i++) {
            p2 = p2.next;

        }

        while (p2 != null) {
            p1 = p1.next;

            p2 = p2.next;

        }
        p1.next = p1.next.next;
        return s.next;


    }


    public static void main(String[] args) {

    }

//    public static void main(String[] args) {
//
//        LinkedNode linkedNode5 = new LinkedNode(5, null);
//        LinkedNode linkedNode4 = new LinkedNode(4, linkedNode5);
//        LinkedNode linkedNode3 = new LinkedNode(3, linkedNode4);
//        LinkedNode linkedNode2 = new LinkedNode(2, linkedNode3);
//        LinkedNode linkedNode1 = new LinkedNode(1, linkedNode2);
////        LinkedNode temp=linkedNode1;
////        while (temp!=null){
////            System.out.println(temp.value);
////            temp=temp.next;
////
////        }
//
//        LinkedNode reversedNode = reverseNode(linkedNode1);
//
//        LinkedNode temp=reversedNode;
//        while (temp!=null){
//            System.out.println(temp.value);
//            temp=temp.next;
//        }
//
//    }


}
